package jwebtester.business.beans.competencies.converters;

import infrastructure.Scopes;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import jwebtester.business.services.SessionMapService;
import jwebtester.dal.provider.CompetenciesDataProvider;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import domain.entities.Competency;

@Component
@Scope(Scopes.Request)
public class CompetencyConverter implements Converter {

	@Autowired
	private transient CompetenciesDataProvider dataProvider;

	@Autowired
	private transient SessionMapService sessionService;

	@Override
	public Object getAsObject(final FacesContext context, final UIComponent component,
			final String value) {
		final int organizationID = sessionService.getCurrentOrganizationID();
		return dataProvider.getCompetencyByName(organizationID, value);
	}

	@Override
	public String getAsString(final FacesContext context, final UIComponent component,
			final Object value) {
		return ((Competency) value).getName();
	}

}
